package org.jsola.contract.helper;

import org.jsola.common.SafeKit;
import org.jsola.contract.dto.ContractGroupAddDTO;
import org.jsola.contract.dto.ContractGroupUpdateDTO;
import org.jsola.contract.entity.ContractGroupDO;
import org.jsola.contract.query.ContractGroupQuery;
import org.jsola.contract.service.IContractGroupService;
import org.jsola.contract.vo.ContractGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.contract.core.AbstractTest.SITE_ID;



/**
 * 合同类型分组
 *
 * @author JanusMix
 */
@Component
public class ContractGroupTestHelper extends BaseTestHelper {
    
    @Autowired
    private IContractGroupService contractGroupService;

    /**
     * 生成add测试数据
     * @return ContractGroupAddDTO
     */
    public ContractGroupAddDTO getContractGroupAddDTO(){
        ContractGroupAddDTO contractGroupAddDTO = new ContractGroupAddDTO();
        contractGroupAddDTO.setIsSystem(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        contractGroupAddDTO.setName(getRandomValue("varchar",255));
        contractGroupAddDTO.setSubjectId(getRandomValue("varchar",32));
        contractGroupAddDTO.setSort(SafeKit.getInteger(getRandomValue("int",10)));
        return contractGroupAddDTO;
    }


    /**
     * 生成update测试数据
     * @return ContractGroupUpdateDTO
     */
    public ContractGroupUpdateDTO getContractGroupUpdateDTO(ContractGroupVO contractGroupVO){
        return contractGroupService.selectDOById(contractGroupVO.getId(), SITE_ID).to(ContractGroupUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return ContractGroupQuery
     */
    public ContractGroupQuery getContractGroupQuery() {
        ContractGroupQuery contractGroupQuery = new ContractGroupQuery();
        List<ContractGroupDO> contractGroupDOList = contractGroupService.list(new ContractGroupDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(contractGroupDOList)) {
            ContractGroupDO contractGroupDO = contractGroupDOList.get(0);
            contractGroupQuery = contractGroupDO.to(ContractGroupQuery.class);
        }
        return contractGroupQuery;
    }

    /**
     * 保存示例
     * @return ContractGroupVO
     */
    public ContractGroupVO saveContractGroupExample(){
        ContractGroupAddDTO addDTO = this.getContractGroupAddDTO();
        ContractGroupVO contractGroupVO = contractGroupService.save(addDTO,buildTokenUser());
        return contractGroupVO;
    }


}

